package nc.ui.zzjs.bd.chnlmemtype;

import java.util.Arrays;
import java.util.Collection;
import java.util.Comparator;

import nc.bs.framework.common.NCLocator;
import nc.bs.logging.Logger;
import nc.bs.uap.bd.BDRuntimeException;
import nc.itf.uap.IUAPQueryBS;
import nc.ui.trade.pub.IVOTreeDataByID;
import nc.vo.pub.BusinessException;
import nc.vo.pub.SuperVO;
import nc.vo.zzjs.bd.chnlmemtype.ChnlMemTypeVO;

public class ChnlMemTypeTreeCardData implements IVOTreeDataByID {
	
	private String pk_corp = null;
	
	public ChnlMemTypeTreeCardData (String pk_corp){
		this.pk_corp = pk_corp;
	}	 

	private SuperVO[] m_ChnlMemTypeVOs = null;

	public String getIDFieldName() {
		return "pk_memtype";
	}

	public String getParentIDFieldName() {
		return "pk_parent";
	}

	public String getShowFieldName() {
		return "vname";
	}

	public SuperVO[] getTreeVO() {
		if (m_ChnlMemTypeVOs == null) {
			IUAPQueryBS queryBS = (IUAPQueryBS) NCLocator.getInstance().lookup(IUAPQueryBS.class.getName());
			Collection deptColl = null;
			try {
				StringBuffer strWhere = new StringBuffer(" pk_corp='"+pk_corp+"' and isnull(dr,0) = 0 ");
				deptColl = queryBS.retrieveByClause(ChnlMemTypeVO.class, strWhere.toString());
			} catch (BusinessException e) {
				Logger.error(e.getMessage(), e);
				throw new BDRuntimeException(e.getMessage(), e);
			}
			if (deptColl == null)
				return null;
			m_ChnlMemTypeVOs = (ChnlMemTypeVO[]) deptColl.toArray(new ChnlMemTypeVO[0]);
		}
		Arrays.sort(m_ChnlMemTypeVOs, 
			new Comparator() {
				public int compare(Object o1, Object o2) {
					String code1 = ((ChnlMemTypeVO) o1).getVcode();
					String code2 = ((ChnlMemTypeVO) o2).getVcode();
					return code1.compareTo(code2);
				}
			}
		);
		return m_ChnlMemTypeVOs;
	}
}
